#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9;
const int N = 250;

#define ll long long

int n , m ;
ll graph[N][N], pre[N];
ll flow[N];

ll bfs(int s, int t)
{
    memset(pre, -1, sizeof(pre));
    flow[s] = INF; pre[s] = 0;
    queue<int>Q; Q.push(s);
    while(!Q.empty()){
        int u = Q.front();Q.pop();
        if(u == t)  break;
        for(int i = 1; i <= n; i++)
        {
            if(i != s && graph[u][i] > 0 && pre[i] == -1){
                pre[i] = u;
                Q.push(i);
                flow[i] = min(flow[u], graph[u][i]);
            }
        }
    }

    if (pre[t] == -1)return -1;
    return flow[t];
} 

ll maxflow(int s, int t)
{
    ll Maxflow = 0;
    while(1){
        ll flow = bfs(s, t);
        if(flow == -1)break;
        int cur = t;
        while(cur != s){
            int father = pre[cur];
            graph[father][cur] -= flow;
            graph[cur][father] += flow;
            cur = father;
        }
    Maxflow += flow;
    }
    return Maxflow;
}

int main(){
    int s, t;
    cin>>n>>m>>s>>t;
    memset(graph, 0, sizeof graph);
    for(int i = 0; i < m; i++)
    {
        int u, v, w;
        cin >> u>> v>> w;
        graph[u][v] += w;
    }
    cout<< maxflow(s, t);
    return 0;
}